package com.sky.controller.admin;
import com.sky.constant.JwtClaimsConstant;
import com.sky.context.BaseContext;
import com.sky.dto.*;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "员工管理相关接口")
public class EmployeeController {

    @Autowired

    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;


    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @ApiOperation("管理员登录接口")
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }
    @ApiOperation("修改密码")
    @PutMapping("editPassword")
    public Result editPassword(@RequestBody EmployeeEditPasswordDTO employeeEditPasswordDTO){
        log.info("修改密码：{}", employeeEditPasswordDTO);
        Long currentId = BaseContext.getCurrentId();
        employeeEditPasswordDTO.setEmpId(currentId);
        employeeService.editPassword(employeeEditPasswordDTO);

        return Result.success( );
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    public Result logout() {
        return Result.success();
    }

    /**
     * 添加员工
     *
     * @param employeeDTO
     * @return
     */
    @PostMapping
    @ApiOperation("添加员工接口")
    public Result save(@RequestBody EmployeeDTO employeeDTO) {
        log.info("添加员工: {}", employeeDTO);
        employeeService.save(employeeDTO);
        return Result.success();
    }

    /**
     * 分页查询
     *
     * @param employeePageQueryDTO
     * @return
     */
    @GetMapping("page")
    @ApiOperation("分页查询接口")
    public Result<PageResult> page(EmployeePageQueryDTO employeePageQueryDTO) {
        log.info("员工分页查询接口:{}", employeePageQueryDTO);
        PageResult result = employeeService.poge(employeePageQueryDTO);
        return Result.success(result);
    }

    /**
     * 员工启用禁用接口
     *
     * @param status
     * @param id
     * @return
     */
    @ApiOperation("员工启用禁用")
    @PostMapping("/status/{status}")
    public Result status(@PathVariable Integer status, Long id) {
        log.info("员工启用禁用接口查询:{},{}", status, id);
        employeeService.updete(status, id);
        return Result.success();

    }

    /**
     * 根据id查询员工
     * @param id
     * @return
     */
    @ApiOperation("根据id查询员工")
    @GetMapping("/{id}")
    //@PutMapping("/{id}")
    public Result<Employee> selectid(@PathVariable Long id){
            log.info("根据id查询员工:{}",id);
            Employee employee= employeeService.selectid(id);
            return Result.success(employee);
    }
    @ApiOperation("修改员工")
    @PutMapping()
    public  Result pudeta(@RequestBody EmployeeDTO employeeDTO){
        log.info("修改员工接口:{}",employeeDTO);
        employeeService.upde(employeeDTO);
        return  Result.success();
    }


}
